<template>
  <!-- 人员类型新增 -->
  <div>
    <i class="el-icon-date" @click="handleAdd" />
    <el-dialog :modal="false" :title="dialogTitle" :close-on-click-modal="false" class="dialogStyleSmall" :visible.sync="dialogVisible">
      <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
        <el-form-item label="距离过期" prop="certificateExpire">
          <el-input v-model="formData.certificateExpire" size="medium" placeholder="请输入距离过期提醒天数">
            <template slot="append"><el-button />天</template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="开启提醒">
          <el-switch v-model="formData.isStart" />
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="cancel">取 消</el-button>
        <el-button size="medium" type="primary" @click="submitForm">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { integer } from '@/utils/validate'
import { certOverdueReminder } from '@/api/common/certificateDate'
export default {
  name: 'DetailDialog',
  components: {},
  data() {
    return {
      /* 新增 start */
      btnDisabled: false,
      dialogTitle: '设置证书过期提醒时间', // 对话框是否弹出显示
      dialogVisible: false, // 对话框是否弹出显示
      formData: {
        certificateExpire: '' // 距离过期
      },
      // from表单校验
      rules: {
        certificateExpire: [
          { required: true, message: '距离过期天数不能为空', trigger: 'blur' },
          { required: true, message: '过期天数必须为正整数', validator: integer, trigger: 'blur' }]
      }
      /* 新增 end */
    }
  },
  computed: {
    ...mapState({
      certificateExpire: state => state.user.certificateExpire
    })
  },
  methods: {
    handleAdd() {
      this.dialogVisible = true
      this.formData.certificateExpire = this.certificateExpire && +this.certificateExpire
    },
    // 弹框取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
    },
    // 保存
    submitForm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          await certOverdueReminder(this.formData)
          this.msgSuccess('设置成功')
          this.cancel()
        }
      })
    }
  }
}
</script>

